package com.xuliugen.jwt.demo.controller;

import com.xuliugen.jwt.demo.command.LoginCommand;
import com.xuliugen.jwt.demo.controller.base.BaseController;
import com.xuliugen.jwt.demo.dto.UserDTO;
import com.xuliugen.jwt.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * @author xuliugen
 * @date 2018/1/31
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录请求
     * @param command       登录command
     * @param bindingResult 参数校验结果
     * @return
     */
    @PostMapping(value = "/login")
    public Map<String, Object> login(@RequestBody LoginCommand command, BindingResult bindingResult) {
        super.bindingResultWrapper(bindingResult);
        return userService.login(command);
    }

    /**
     * 获取所有用户
     * @param response 用于设置相应状态码
     * @return
     */
    @GetMapping(value = "/list")
    public List<UserDTO> listUser(HttpServletResponse response) {
        List<UserDTO> users = userService.listUser();
        if (users == null) {
            response.setStatus(HttpStatus.NO_CONTENT.value());
        }
        return users;
    }
}
